using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using CubeWars___Demolition.Services;
using CubeWars___Demolition.PhysicObjects;
using CubeWars___Demolition.Controllers;
using CubeWars___Demolition.GamePlay;
using CubeWars___Demolition.Utils;
using Physics.Physics;
using Physics.Collision;
using Physics.Geometry;
using Physics.Math;
using Physics.Utils;
using Physics.Vehicles;

namespace CubeWars___Demolition
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        public gameState gameState;
        Camera camera;
        viewController viewController;
        huds huds;
        FrameRateCounter frameRatecounter;

        PhysicsSystem physicSystem;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            physicSystem = new PhysicsSystem();
            physicSystem.CollisionSystem = new CollisionSystemSAP();

            physicSystem.EnableFreezing = true;
            physicSystem.SolverType = PhysicsSystem.Solver.Normal;
            physicSystem.CollisionSystem.UseSweepTests = true;

            physicSystem.NumCollisionIterations = 4;
            physicSystem.NumContactIterations = 14;
            physicSystem.NumPenetrationRelaxtionTimesteps = 1;

            camera = new Camera(this);
            Components.Add(camera);
            viewController = new viewController(this);
            Components.Add(viewController);
            huds = new huds(this);
            Components.Add(huds);
            huds.DrawOrder = 2;

            frameRatecounter = new Utils.FrameRateCounter(this, physicSystem);
            Components.Add(frameRatecounter);
            frameRatecounter.DrawOrder = 3;

            gameState = new gameState();
            Services.AddService(typeof(gameState), gameState);

            TargetElapsedTime = TimeSpan.FromTicks(333333);

            graphics.PreferredBackBufferHeight = 480;
            graphics.PreferredBackBufferWidth = 800;
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            //GamePlay.CreateScene.CreateScene2(this, 1) ;
            //CreateScene.createFloor(this);
            var a = new Levels.Level1(this, 4);
            Components.Add(a);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            frameRatecounter.mario = "Components: " + Components.Count;

            float timeStep = (float)gameTime.ElapsedGameTime.Ticks / TimeSpan.TicksPerSecond;
            if (timeStep < 1.0f / 30.0f) 
                physicSystem.Integrate(timeStep);
            else 
                physicSystem.Integrate(1.0f / 30.0f);

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);

            base.Draw(gameTime);
        }
    }
}
